package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.been.AuditAppInfo;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.db.SDBaseSql;

import java.util.ArrayList;
import java.util.List;



public class AuditAppDBBiz extends BaseSql {

	private String appInfoTable = "audit_appinfo_msg";

	private String appInfo = "appInfo";

    private String appId="appId";
	public AuditAppDBBiz(MyDBHelper db) {
		super(db);
	}

	public AuditAppDBBiz() {

	}

	@Override
	public void create(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + appInfoTable + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," +appId+" CHAR(255),"+ appInfo + " BLOB);");

	}

	public void dropTable(SQLiteDatabase db) {
		db.execSQL(TB_DELETE + appInfoTable);
	}

	@Override
	public void upgrade(SQLiteDatabase db) {
	}

	/**
	 * 保存sort数据到本地数据库
	 */
	public int saveAppInfo(AuditAppInfo mBean) {
		where = appId+"='"+mBean.id+"'";
		values = new ContentValues();
		values.put(appId, mBean.id);
		values.put(appInfo, getBytesFromObject(mBean));
		return super.saveOrUpdate(appInfoTable);

	}

	/**
	 * 查询保存的app信息
	 */
	public List<AuditAppInfo> queryAppInfo() {
		List<AuditAppInfo> list = new ArrayList<AuditAppInfo>();

		try {
			Cursor cur = db.query(appInfoTable, null);

			if (cur != null) {
				for (int i = 0; i < cur.getCount(); i++) {
					if (cur.moveToPosition(i)) {
						AuditAppInfo bean = (AuditAppInfo) getObjectFromBytes(cur.getBlob(cur
								.getColumnIndex(appInfo)));
						list.add(bean);
					}
				}
				cur.close();
			}
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			db.close();
		}

		return list;
	}
	/**
	 * 删除保存的sort信息
	 */
	public int deletAppInfo(String id) {
		selection = appId + "='" + id + "'";
		return super.delete(appInfoTable);
	}

}
